<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/config.inc.php');
include_once("config.php");
getConfig();
checkActiveLogin();
getUserInfo();
$CONFIG['current_mysql_credentials']['mysql_database'] = 'k103';
db::disconnect();
if($USER['sLevel'] == 'Administrator' or $USER['sLevel'] == 'Super Administrator'){
  # Original PHP code by Chirp Internet: www.chirp.com.au
  # Please acknowledge use of this code by including this header.

  function cleanData(&$str)
  {
    $str = preg_replace("/\t/", "\\t", $str);
    $str = preg_replace("/\r?\n/", "\\n", $str);
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  }

  # file name for download
  $filename = "K103_" . date('Ymd') . ".xls";
  
  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.ms-excel");
  
//  header("Content-Type: text/plain");

  $flag = false;
  $result = db::Query("SELECT sTime, sDate, sName, sClass FROM `tbKLabVisits` ORDER BY `iID`") or die('Query failed!');
  while($row = $result->fetch()) {
    if(!$flag) {
      # display field/column names as first row
      //echo implode("\t", array_keys($row)) . "\n";
      $flag = true;
	  foreach($row as $key=>$value){
		if(!is_numeric($key)){
			echo $key."\t";
		}
	  }
	  echo "\n";
    }
   // array_walk($row, 'cleanData');
	//echo implode("\t", array_values($row)) . "\n";
	foreach($row as $key=>$value){
		if(!is_numeric($key)){
			echo $value."\t";
		}
	}
	echo "\n";
  }
}
?>
<?php
include("template_bot.php");
?>